<?php

namespace WenRuns\Laravel\Admin\Form\Field\Grid\Field;

use WenRuns\Laravel\Admin\Form\Field\Grid\Column;
use WenRuns\Laravel\Admin\Form\Field\Grid\Grid;
use WenRuns\Laravel\Admin\Form\Field\Grid\Field\Number;

/**
 * @method Input input($column, $label = '')
 * @method Select select($column, $label = '')
 * @method Number number($column, $label = '')
 * @method Textarea textarea($column, $label = '')
 * @method Radio radio($column, $label = '')
 * @method Checkbox checkbox($column, $label = '')
 * @method Expand expand($column, $label, $content)
 * @method Modal modal($column, $label = '', $content = null, $title = '')
 * @method Color color($column, $label = '')
 * @method Decimal decimal($column, $label = '')
 * @method Display display($column, $label = '')
 * @method Hidden hidden($column, $label = '')
 * @method Time time($column, $label = '')
 * @method Date date($column, $label = '')
 * @method DateTime datetime($column, $label = '')
 * @method PowerSwitch switch ($column, $label = '')
 * @method Image image($column, $label = '')
 * @method Html html($label, $html)
 * @method Group group($label, \Closure $closure)
 * @method Email email($column, $label)
 * @method Phone phone($column, $label)
 * @method Password password($column, $label)
 * @method Range range($column, $label)
 * @method Tab tab(\Closure $closure)
 */
class TabRow extends Column
{
    /**
     * @var
     */
    protected $title;
    /**
     * @var \Closure
     */
    protected $closure;

    protected $type = 'tabRow';

    /**
     * @var array
     */
    protected $columns = [];

    /**
     * @var Tab
     */
    protected $tab;

    /**
     * @var bool
     */
    protected $priority = false;

    /**
     * @param Grid $grid
     * @param $title
     * @param \Closure $closure
     * @param $name
     */
    public function __construct(Grid $grid, $title, \Closure $closure, Tab $tab, $name = null)
    {
        $this->tab = $tab;
        $this->title = $title;
        $this->closure = $closure;
        parent::__construct($grid, $name, null);
        call_user_func($closure, $this);
    }


    public function isActive()
    {
        $this->tab->default(empty($this->column) ? $this->title : $this->column);
        return $this;
    }

    /**
     * @return array
     */
    public function render()
    {
        $res = parent::render(); // TODO: Change the autogenerated stub
        $res['title'] = $this->title;
        $res['columns'] = $this->children();
        return $res;
    }

    /**
     * @return array
     */
    protected function children()
    {
        $children = [];
        foreach ($this->columns as $column) {
            if ($column instanceof Grid) {
                $children[] = [
                    'label' => $column->label(),
                    'type' => 'grid',
                    'name' => $column->column(),
                    'options' => $column->initOption(),
                ];
            } else {
                /** @var Column $column */
                $children[] = $column->render();
            }
        }
        return $children;
    }


    public function grid($column, \Closure $closure, $label = '')
    {
        $grid = new Grid($column, [$closure, $label], $this);
        $this->columns[] = $grid;
        call_user_func($closure, $grid);
        return $grid;
    }


    /**
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \Exception
     */
    public function __call($name, $arguments)
    {
        return Grid::checkColumn($name, $arguments, function ($column, $parameters) {
            $instance = new $column($this->grid, ...$parameters);
            $this->columns[] = $instance;
            return $instance;
        });
    }

    public function columnWidth($width = 6)
    {
        $this->priority = true;
        return $this->config('columnWidth', $width);
    }

    public function priority()
    {
        return $this->priority;
    }
}
